/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hvtt.web.controller;

import com.hvtt.web.controllerUtils.MainController;
import com.hvtt.web.utilities.Utilities;
import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.panel.Panel;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class HandlerExceptionController implements Serializable{
    private String images = "images/warning.png";
    private int codeError = 0;
    private String classNameError = "";
    private String messageError = "";
    private String fullMessageError = "";
    private String serverName = "";
    private boolean isRender = false;
    private boolean isDetails = false;
    private String includePage = "ui/inbox.xhtml";
    private String historyIncludePage = "";

    public HandlerExceptionController() {
    }

    public String getIncludePage() {
        return includePage;
    }

    public void setIncludePage(String includePage) {
        this.setHistoryIncludePage(this.getIncludePage());
        this.includePage = includePage;
    }

    public void setHistoryIncludePage(String historyIncludePage) {
        this.historyIncludePage = historyIncludePage;
    }

    public String getHistoryIncludePage() {
        return historyIncludePage;
    }

    public boolean isIsDetails() {
        return isDetails;
    }

    public void setIsDetails(boolean isDetails) {
        this.isDetails = isDetails;
    }

    public boolean isIsRender() {
        return isRender;
    }

    public void setIsRender(boolean isRender) {
        this.isRender = isRender;
    }

    public String getClassNameError() {
        return classNameError;
    }

    public void setClassNameError(String classNameError) {
        this.classNameError = classNameError;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    public String getServerName() {
        this.setServerName(FacesContext.getCurrentInstance().getExternalContext().getRequestServerName());
        return serverName;
    }

    public String getFullMessageError() {
        return fullMessageError;
    }

    public void setFullMessageError(String fullMessageError) {
        this.fullMessageError = fullMessageError;
    }
    public String getImages() {
        return images;
    }

    public void setImages(String images) {
        this.images = images;
    }

    public int getCodeError() {
        return codeError;
    }

    public void setCodeError(int codeError) {
        this.codeError = codeError;
    }

    public String getMessageError() {
        return messageError;
    }

    public void setMessageError(String messageError) {
        this.messageError = messageError;
    }

    public void showDetailsException(ActionEvent event) {
        if (!isDetails) {
            CommandButton button = (CommandButton) event.getSource();
            button.setValue("<< Hide details");
            Panel panel = (Panel) FacesContext.getCurrentInstance().getViewRoot().findComponent("contentForm:outputErrorDetails");
            panel.getChildren().clear();
            HtmlOutputText outputText = new HtmlOutputText();
            outputText.setValue(this.getFullMessageError());
            panel.getChildren().add(outputText);
            this.setIsDetails(true);
            this.setIsRender(true);
        } else {
            CommandButton button = (CommandButton) event.getSource();
            button.setValue("Show details >>");
            Panel panel = (Panel) FacesContext.getCurrentInstance().getViewRoot().findComponent("contentForm:outputErrorDetails");
            panel.getChildren().clear();
            this.setIsDetails(false);
            this.setIsRender(false);
        }
    }

    public void reloadHistoryPage(ActionEvent event) {
        FacesContext context = FacesContext.getCurrentInstance();
        MainController mainController = Utilities.getManagedBean(MainController.class);
        mainController.setIncludePage(mainController.getHistoryIncludePage());
        String uri = ((HttpServletRequest) context.getExternalContext().getRequest()).getRequestURI();
        System.out.println(uri);
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect(uri);
        } catch (IOException ex) {
            Logger.getLogger(HandlerExceptionController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
